App.controller('myOrderRefundController', ['$rootScope', '$scope', '$state', '$window', 'Page',function($rootScope, $scope, $state, $window, Page) {
	
	$scope.orderId = $state.params.orderId;
	$scope.order = {};
	
    Page.ajaxPost("app/order/canBackDetails.do",{orderId : $scope.orderId},function(resp){
        $scope.order = resp.data;
		Page.ajaxPost("line/stops.do",{lineId : $scope.order.lineId},function(resp){
			$scope.stopList = resp.data;
		});
    });
	
	$scope.orderRefund = function(){
		var backInfos = [];
		$($scope.order.passengers).each(function(idx, passenger){
			var passengerObj = null;
			var flag = false;
			$(backInfos).each(function(i, info){
				  if(info.passengerPhone == passenger.passengerPhone){
					  passengerObj = info;
					  return false;
				  }
			}); 
			if(passengerObj == null){
				passengerObj = {
					passengerPhone: passenger.passengerPhone,
					passengerName: passenger.passengerName,
					dates : []
				};
				flag = true;
			}
			
			$(passenger.dates).each(function(i, date){
				  if(date && date.selected){
					  passengerObj.dates.push(date);
				  }
			}); 
			if(flag){
				backInfos.push(passengerObj);
			}
		});


		var canApplyFlag = false;
		for (var i = 0; i < backInfos.length; i++) {
			var obj = backInfos[i];
			//只要有一个数组中包含日期就可以退款
			if(obj.dates.length > 0) {
				canApplyFlag = true;
				break;
			} else {
				canApplyFlag = false;
				continue;
			}
		}
		if(!canApplyFlag) {
			Page.notice("请选择退订日期!");
			return;
		}

		Page.ajaxPost("app/orderBack/getCanRefundAmount.do",{orderId : $scope.order.orderId,
            backInfos: backInfos},function(resp){
            var price = resp.data.price;
            Page.confirm("退款金额为:" + price + " ￥, 确定要退款吗?", function () {
                Page.ajaxPost("app/orderBack/apply.do",{orderId : $scope.order.orderId,orderBackType : '1',
                    backInfos:backInfos},function(resp){
                    Page.alert("已发送退款申请!请等管理员通过!",function(){
                    	$state.go("home");
                    });
                });
            });
        });
	};
	
}]);